<?php
ob_start();
session_start();

function __autoload($class_name) {
include './'.$class_name . '.php';
}
$productId=$_GET['productId']; 
$size=$_GET['size'];
$quantity=$_GET['quantity'];
$productName = $_GET['productName'];
$rate = null;
$dbConfig = new DBConfig(); 
$conn = $dbConfig->getConnection();
$sql="select p1.price from products p1 where p1.id_product= ".$productId;

if ($result = $conn->query($sql)) {
	$row = $result->fetch_array();    /* free result set */
	$rate=$row['price'];
	$result->close();
}

$dbConfig->closeConnection();

$rate=$_GET['rate'];
$cart = NULL;
if(!isset($_SESSION['cart'])){
$cart = new OrderList(); 		
}else{
$cart = $_SESSION['cart'];
}

$myOrder = NULL;
$flag = 0;
foreach($cart->getOrderList() as $myOrder){
	$mySize = $myOrder->getSize();
	$myProductId = $myOrder->getProductId();
	if(($myProductId == $productId) && ($mySize == $size)){
		$flag = 1;
		$myOrder->setQuantity($myOrder->getQuantity() + $quantity);
		break;
	}	
}

if( $flag == 0){
	$order = new Order();
	$order->setProductId($productId);
	$order->setSize($size);
	$order->setQuantity($quantity);
	$order->setProductName($productName);
	$order->setRate($rate);
	$cart->insertOrder($order);
	
}

$_SESSION['cart']=$cart;
echo $cart->countOrder();
ob_end_flush();
?>